/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atm.service;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.atm.domain.Account;

/**
 *
 * @author Rahul
 */
@Stateless
public class AccountServiceImpl implements AccountService {
    @PersistenceContext(unitName = "comp-5541-atmPU")
    private EntityManager entityManager;
    
    public void create(Account account) {
        entityManager.persist(account);
    }
    
    public Account getById(int accountId) {
        return entityManager.find(Account.class, accountId);
    }
    
    public List<Account> getAll() {
        return entityManager.createNamedQuery("Account.findAll", Account.class).getResultList();
    }
    public void activateAccount(Account account) {
        account.setActivate();
        entityManager.merge(account);
    }
    public void deactivateAccount(Account account) {
        account.setDeactivate();
        entityManager.merge(account);
    }
    public Account getByAccountNumber(Integer number) {
        return entityManager.createNamedQuery("Account.findByAccountNumber", Account.class).
                setParameter("number", number).
                getSingleResult();
    }

  public void persist(Account account) {

        entityManager.merge(account);
    }
}
